package com.winderinfo.yikaotianxia.ui.study;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.videolearning.LearningActivity;
import com.winderinfo.yikaotianxia.ui.xiangqin.DetailsActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class StudyFragment extends BaseFragment implements Contract.View {
    @BindView(R.id.st_recycle)
    RecyclerView stRecycle;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_study;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(getActivity());
        stRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void initData() {
        //获取用户Id
        String userID = spUtils.getString("UserID", "");
        if (userID != null) {
            /**
             * @id 用户Id
             * */
            map.clear();
            map.put("id", 1);
            //请求学习列表
            presenter.onPostRequest(Api.XUEXI, map, StudyBean.class);
        } else {
            ToastUtil.showSuccess(getActivity(), "请先登录");
        }

    }

    @Override
    public void onSuccess(Object data) {
        //学习列表
        if (data instanceof StudyBean) {
            List<StudyBean.CoursesBean> courses = ((StudyBean) data).getCourses();
            StudyAdapter studyAdapter = new StudyAdapter(getActivity(), courses);
            stRecycle.setAdapter(studyAdapter);
            //接口回调
            studyAdapter.setOnGoodsId(new StudyAdapter.OnGoodsId() {
                @Override
                public void getId(int id, String details, String photo) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("id", id);
                    bundle.putString("details", details);
                    bundle.putString("photo", photo);
                    openActivity(LearningActivity.class, bundle);
                }
            });
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

}
